package com.qf.java2105.huangchuang.hotel.dao.impl;

import com.qf.java2105.huangchuang.hotel.dao.IFoodTypeDao;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 菜系持久层实现
 * @author hcvg
 * @version v1.0
 * @date 2021/09/11
 **/
public class FoodTypeDaoImpl implements IFoodTypeDao {
    private QueryRunner queryRunner = null;

    @Override
    public List<FoodType> findByFoodTypeName(String foodTypeName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "SELECT\n" +
                "  type_id typeId,\n" +
                "  type_name typeName\n" +
                "FROM\n" +
                "  t_food_type WHERE type_name like ?";

        if (null == foodTypeName){
            return queryRunner.query(sql, new BeanListHandler<>(FoodType.class), "%%");
        }
        return queryRunner.query(sql, new BeanListHandler<>(FoodType.class), "%" + foodTypeName + "%");
    }

    @Override
    public FoodType findFoodTypeById(Integer typeId) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "SELECT\n" +
                "  type_id typeId,\n" +
                "  type_name typeName\n" +
                "FROM\n" +
                "  t_food_type WHERE type_id = ?";
        return queryRunner.query(sql, new BeanHandler<>(FoodType.class), typeId);
    }

    @Override
    public void update(Integer foodTypeId, String foodTypeName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "UPDATE\n" +
                "  t_food_type\n" +
                "SET\n" +
                "  type_name = ?\n" +
                "WHERE type_id = ?";
        queryRunner.update(sql, foodTypeName, foodTypeId);
    }

    @Override
    public void delete(Integer typeId) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "DELETE FROM t_food_type WHERE type_id = ?";
        queryRunner.update(sql, typeId);
    }

    @Override
    public void save(String typeName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "INSERT INTO t_food_type (type_name)\n" +
                "VALUES\n" +
                "  (" +
                "  ?" +
                "  )";
        queryRunner.update(JdbcUtil.getConnection(),sql, typeName);
    }

    @Override
    public Integer foodTypeExist(String typeName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "SELECT type_id FROM t_food_type WHERE type_name = ?";
        return queryRunner.query(sql, new ScalarHandler<>(), typeName);
    }
}
